package work.base.designpattern.responsibility;

// 抽象处理者：领导
abstract class Leader {
    private Leader nextLeader; // 下一个处理者

    // 设置下一个处理者
    public void setNextLeader(Leader nextLeader) {
        this.nextLeader = nextLeader;
    }

    // 处理请求的方法
    public final void handleRequest(int days) {
        if (days <= limit()) {
            handle(days);
        } else {
            if (nextLeader != null) {
                nextLeader.handleRequest(days);
            } else {
                System.out.println("请假天数太多，无人处理");
            }
        }
    }

    // 每个领导能批准的天数上限
    protected abstract int limit();

    // 具体处理请求的方法
    protected abstract void handle(int days);
}

// 具体处理者：主任
class Director extends Leader {
    @Override
    protected int limit() {
        return 3;
    }

    @Override
    protected void handle(int days) {
        System.out.println("主任批准了" + days + "天假");
    }
}

// 具体处理者：经理
class Manager extends Leader {
    @Override
    protected int limit() {
        return 10;
    }

    @Override
    protected void handle(int days) {
        System.out.println("经理批准了" + days + "天假");
    }
}

// 具体处理者：总经理
class GeneralManager extends Leader {
    @Override
    protected int limit() {
        return 30;
    }

    @Override
    protected void handle(int days) {
        System.out.println("总经理批准了" + days + "天假");
    }
}

// 客户端
public class ResponsibilityChain {
    public static void main(String[] args) {
        // 创建责任链
        Leader director = new Director();
        Leader manager = new Manager();
        Leader generalManager = new GeneralManager();

        // 设置责任链顺序：主任 -> 经理 -> 总经理
        director.setNextLeader(manager);
        manager.setNextLeader(generalManager);

        // 提交请求
        director.handleRequest(2);
        director.handleRequest(7);
        director.handleRequest(20);
        director.handleRequest(40);
    }
}

